<?php
/**
 * Created by PhpStorm.
 * User: lance
 * Date: 2017/11/1
 * Time: 16:27
 */

namespace app\log\admin;

use app\common\builder\ZBuilder;
use think\Db;

/**
 * 客户日志
 * Class Customer
 * @package app\log\admin
 */
class Customer extends Common
{
    /**
     * 客户系新增日志
     */
    public function showAddLog()
    {
        $a_where = [
            'role_type' => 1,//1代表客户
            'a.action' => ['lt',7]
        ];
        //客户经理只能看自己操作的客户
        if ($this->i_role === 3):
            $a_where = [
                'a.operate_id' => $this->i_uid,
            ];
        endif;
        $a_where = $this->getMap() + $a_where;
        $a_field = [
            'name', 'sex', 'cert_type', 'cert_number', 'action', 'time', 'status', 'a.id id'
        ];
        $s_tips = <<<EOT
1.操作栏，可查看更新客户的操作人员、以及更新客户的信息(更新成功的客户会显示最新的客户信息，失败时只会显示最初更新客户时的信息)
EOT;

        $a_log = Db::name('role_log a')->field($a_field)->join('customer b', 'a.role_id=b.id', 'LEFT')->where($a_where)->order('time desc')->paginate();
        return ZBuilder::make('table')
            ->hideCheckbox()
            ->addFilter('status', ['失败', '成功'])
            ->setSearch([
                'b.name' => '姓名',
                'b.cert_number' => '证件号码'
            ])
            ->setPageTips($s_tips)
            ->addTimeFilter('time')
            ->setTableName('role_log')
            ->addColumns(config('customer_log'))
            ->addTrClass('danger', 'status', 0)
            ->addTrClass('success', 'status', 1)
            ->addRightButton('custom',
                [
                    'title' => '查看详情',
                    'icon' => 'glyphicon glyphicon-eye-open',
                    'href' => url('showadddetail', [
                        'id' => '__id__'
                    ]),
                ], true)
            ->setRowList($a_log)
            ->fetch();
    }
    public function showUpdateLog(){
        $a_where = [
            'role_type' => 1,//1代表客户
            'a.action' => ['egt',7]
        ];
        //客户经理只能看自己操作的客户
        if ($this->i_role === 3):
            $a_where = [
                'a.operate_id' => $this->i_uid,
            ];
        endif;
        $a_where = $this->getMap() + $a_where;
        $a_field = [
            'name', 'sex', 'cert_type', 'cert_number', 'action', 'time', 'status', 'a.id id'
        ];
        $s_tips = <<<EOT
1.操作栏，可查看新增客户的操作人员、以及新增客户的信息(添加成功的客户会显示最新的客户信息，失败时只会显示最初添加客户的信息)
EOT;

        $a_log = Db::name('role_log a')->field($a_field)->join('customer b', 'a.role_id=b.id', 'LEFT')->where($a_where)->order('time desc')->paginate();
        return ZBuilder::make('table')
            ->hideCheckbox()
            ->addFilter('status', ['失败', '成功'])
            ->setSearch([
                'b.name' => '姓名',
                'b.cert_number' => '证件号码'
            ])
            ->setPageTips($s_tips)
            ->addTimeFilter('time')
            ->setTableName('role_log')
            ->addColumns(config('customer_log'))
            ->addTrClass('danger', 'status', 0)
            ->addTrClass('success', 'status', 1)
            ->addRightButton('custom',
                [
                    'title' => '查看详情',
                    'icon' => 'glyphicon glyphicon-eye-open',
                    'href' => url('showadddetail', [
                        'id' => '__id__'
                    ]),
                ], true)
            ->setRowList($a_log)
            ->fetch();
    }
    /**
     * 客户添加详情
     */

    function showAddDetail()
    {
        $a_log = Db::name('role_log')->where('id', input('id/d', 0))->find();
        //描述
        $a_operate = Db::name('admin_user')->where('id', $a_log['operate_id'])->find();
        $a_role = Db::name('admin_role')->where('id', $a_operate['role'])->find();
        $a_content = json_decode($a_log['content'], true);
        $a_log['status'] = $a_log['status'] ? '<span style="color:green">成功</span>' : '<span style="color:red">失败</span>';
        $s_message = "操作时间：" . date('Y-m-d H:i:s', $a_log['time'])
            . '<br>状态：' . $a_log['status']
            . '<br>操作人员：' . $a_operate['username'] . '(' . $a_role['name'] . ')'
            . '<br>描述：' . config('customer_log')[5][4][$a_log['action']]
            . '<br>错误信息：' . $a_log['error'];
        //客户信息
        $a_customer = $a_log['status'] ? Db::name('customer')->where('id', $a_log['role_id'])->find() : $a_content;
        $s_customer = 'ID：' . $a_customer['id']
            . '<br>姓名：' . $a_customer['name'] . '<br>性别：' . $a_customer['sex']
            . '<br>证件类型：' . $a_customer['cert_type'] . '<br>证件号码：' . $a_customer['cert_number']
            . '<br>生日：' . $a_customer['birthday'] . '<br>联系电话：' . $a_customer['tel1']
            . '<br>其他号码：' . $a_customer['tel2'] . '<br>行业：' . $a_customer['trade']
            . '<br>职业：' . $a_customer['career'] . '<br>性格：' . $a_customer['nature']
            . '<br>兴趣特长：' . $a_customer['hobbies'];

        return $this->fetch('',
            [
                'customer' => $s_customer,
                'message' => $s_message
            ]);
    }

    /**
     * 客户分配日志
     */
    public function showDisLog()
    {
        $a_where = [];
        //客户经理只能看自己的客户
        if ($this->i_role === 3):
            $a_where = [
                'a.manager_id' => MANAGER_ID,
            ];
        endif;
        $a_fields = [
            'a.id as id', 'b.name as customer', 'c.name as manager', 'a.time as time', 'b.id as cid',
            'c.id as mid', 'a.status as status', 'a.message as message', 'a.error as error'
        ];
        $s_fields = join(',', $a_fields);
        $a_map = $this->getMap();
        foreach ($a_map as $s_key => $a_val):
            switch ($s_key):
                case 'customer':
                    $a_map['b.name'] = $a_val;
                    unset($a_map[$s_key]);
                    break;
                case 'manager':
                    $a_map['b.name'] = $a_val;
                    unset($a_map[$s_key]);
                    break;
            endswitch;
        endforeach;
        $a_where = $a_map + $a_where;
        $a_log = Db::name('distribute_customer_log a')->field($s_fields)
            ->order('a.time desc')
            ->join('customer b', 'a.customer_id=b.id', 'LEFT')
            ->join('manager c', 'a.manager_id=c.id', 'LEFT')
            ->where($a_where)->paginate();
        return ZBuilder::make('table')
            ->hideCheckbox()
            ->setPageTips("可在操作栏查看详细的分配日志信息")
            ->addColumns(config('distribute_log'))
            ->addTrClass('danger', 'status', 0)
            ->addTrClass('success', 'status', 1)
            ->addFilter([
                'status'
            ],['失败','成功'])
            ->setTableName('distribute_customer_log')
            ->addRightButton('detail',
                [
                    'title' => '查看客户分配详情',
                    'icon' => 'glyphicon glyphicon-eye-open',
                    'href' => url('showDisDetail', ['id' => '__id__'])
                ], true)
            ->setSearch([
                'customer' => '客户',
                'manager' => '客户经理'
            ])
            ->setRowList($a_log)
            ->fetch();
    }

    /**
     * 查看客户分配详情
     */
    public function showDisDetail()
    {
        $i_id = input('id/d', 0);
        $a_disData = Db::name('distribute_customer_log')->where('id', $i_id)->find();
        $s_content = $a_disData['content'];
        $a_content = json_decode($s_content, true);
        //客户信息
        $a_customer = Db::name('customer')->where('id', $a_content['customer_id'])->find();
        $s_customer = 'ID：' . $a_customer['id']
            . '<br>姓名：' . $a_customer['name'] . '<br>性别：' . $a_customer['sex']
            . '<br>证件类型：' . $a_customer['cert_type'] . '<br>证件号码：' . $a_customer['cert_number']
            . '<br>生日：' . $a_customer['birthday'] . '<br>联系电话：' . $a_customer['tel1']
            . '<br>其他号码：' . $a_customer['tel2'] . '<br>行业：' . $a_customer['trade']
            . '<br>职业：' . $a_customer['career'] . '<br>性格：' . $a_customer['nature']
            . '<br>兴趣特长：' . $a_customer['hobbies'];
        //经理信息
        $a_manager = Db::name('manager')->where('id', $a_content['manager_id'])->find();
        $s_manager = 'ID：' . $a_manager['id']
            . '<br>部门：' . $a_manager['department']
            . '<br>姓名：' . $a_manager['name'] . '<br>性别：' . $a_manager['sex']
            . '<br>柜员号：' . $a_manager['number'] . '<br>身份证号：' . $a_manager['cert_number']
            . '<br>联系电话：' . $a_manager['tel_number'] . '<br>办公电话：' . $a_manager['office_number']
            . '<br>邮箱：' . $a_manager['email'];
        //描述
        $a_operate = Db::name('admin_user')->where('id', $a_disData['operate_id'])->find();
        $a_role = Db::name('admin_role')->where('id', $a_operate['role'])->find();
        $a_disData['status'] = $a_disData['status'] ? '<span style="color:green">成功</span>' : '<span style="color:red">失败</span>';
        $s_message = "分配时间：" . date('Y-m-d H:i:s', $a_disData['time'])
            . '<br>状态：' . $a_disData['status']
            . '<br>操作人员：' . $a_operate['username'] . '(' . $a_role['name'] . ')'
            . '<br>描述：' . $a_disData['message']
            . '<br>错误信息：' . $a_disData['error'];
        $this->assign([
            'customer' => $s_customer,
            'manager' => $s_manager,
            'message' => $s_message
        ]);
        return $this->fetch();
    }

    /**
     * 客户移交日志
     */
    function showTurnLog()
    {
        $a_where = $a_whereOr = [];
        //客户经理只能看自己的客户移交日志
        if (ROLE_ID == 3):
            $a_where['a.manager_id'] = Db::name('admin_user')->where('id', UID)->value('role_id');
            $a_whereOr = ['a.operate_id' => UID];
        endif;
        $a_map = $this->getMap();
        foreach ($a_map as $s_key => $a_val):
            switch ($s_key):
                case 'customer.name':
                    $a_map['b.name'] = $a_val;
                    unset($a_map[$s_key]);
                    break;
                case 'manager.name':
                    $a_map['c.name'] = $a_val;
                    unset($a_map[$s_key]);
                    break;
                case 'customer_turn_log.status':
                    $a_map['a.status'] = $a_val;
                    unset($a_map[$s_key]);
                    break;
                case 'time':
                    $a_map['a.start_time'] = $a_val;
                    unset($a_map[$s_key]);
                    break;
            endswitch;
        endforeach;
        $a_where = array_merge($a_map, $a_where);//筛选
        $a_fields = [
            'a.id id', 'b.name customer_name', 'c.name manager_name', 'a.time time', 'a.status status', 'a.message message',
            'a.start_time start_time', 'a.old_manager'
        ];
        $a_data = Db::name('customer_turn_log a')
            ->field($a_fields)
            ->order('a.id desc')
            ->join('customer b', 'a.customer_id=b.id')
            ->join('manager c', 'a.manager_id=c.id')
            ->where($a_where)->whereOr($a_whereOr)->order('time desc')->paginate();
        $a_manager = Db::name('manager')->where('type', 0)->column('id,name');
        return ZBuilder::make('table')
            ->hideCheckbox()
            ->addFilter([
                'customer_name' => 'customer.name',
                'manager_name' => 'manager.name',
            ])
            ->addFilter('old_manager', $a_manager)
            ->setTableName('customer_turn_log')
            ->addFilter(['status' => 'customer_turn_log'], ['正在移交', '移交完成','拒绝接收'], '', 'radio')
            ->setSearch([
                'customer.name' => '移交客户',
                'manager.name' => '移交客户经理'
            ])
            ->addFilterMap('customer_name', 'manager_name')
            ->addColumns([
                ['old_manager', '原客户经理', $a_manager],
                ['customer_name', '移交客户'],
                ['manager_name', '移交客户经理'],
                ['start_time', '移交申请时间', 'datetime'],
                ['time', '移交完成时间', 'datetime'],
                ['status', '移交状态', 'status', '', ['正在移交', '移交完成','拒绝接收']],
                ['message', '备注']
            ])
            ->addTimeFilter('start_time', '', ['申请开始时间', '申请结束时间'])
            ->setRowList($a_data)
            ->fetch();
    }

}